<template>
  <view>
    <view class="boxt_box">
      <text>语音提示</text>
      <u-switch v-model="yuyinvalue" @change="change"></u-switch>
    </view>
    <view class="boxt_box">
      <text>消息提示</text>
      <u-switch v-model="xiaoxivalue" @change="change"></u-switch>
    </view>
    <view class="boxt_box">
      <text>系统消息</text>
      <u-switch v-model="xitongvalue" @change="change"></u-switch>
    </view>
    <view class="success_key" @click="setting()">
      <view class="success_fl">播报时间</view>
      <view class="success_fr">
        <text>{{ timename }}</text>
        <image src="../../static/iocn11.png"></image>
      </view>
    </view>
    <view class="footer">
      <u-button type="error" @click="queren()" text="确认"></u-button>
    </view>
  </view>
</template>

<script>
import { SetUserMsgCfg, GetUserMsgCfg } from "@/public/Api/update.js";
import { setValue, getValue } from "@/public/storeage/index.js";
export default {
  data() {
    return {
      yuyinvalue: true,
      xiaoxivalue: true,
      xitongvalue: true,
      time: "300",
      timename: "",
    };
  },
  onLoad() {
    this.GetUserMsgCfg();
    uni.$on("times", (data) => {
      this.time = data.time;
      this.timename = data.detail;
    });
  },
  onShow() {},
  methods: {
    change(e) {
      this.value = e;
    },
    setting() {
      uni.navigateTo({
        url: "/pages/PersonalCenter/timesetting",
      });
    },
    async SetUserMsgCfg() {
      var userId = getValue("id");
      var enableTTS = this.yuyinvalue;
      var ttsInterval = this.time;
      var enableSysMsg = this.xitongvalue;
      var enableBillMsg = this.xiaoxivalue;
      try {
        const { data: res } = await SetUserMsgCfg({
          userId,
          enableTTS,
          ttsInterval,
          enableSysMsg,
          enableBillMsg,
        });
        this.GetUserMsgCfg();
      } catch (e) {}
    },
    async GetUserMsgCfg() {
      var userId = getValue("id");
      try {
        const { data: res } = await GetUserMsgCfg({
          userId,
        });
        this.yuyinvalue = res.result.enableTTS;
        this.xiaoxivalue = res.result.enableBillMsg;
        this.xitongvalue = res.result.enableSysMsg;
        if (res.result.ttsInterval == 300) {
          this.timename = "默认";
        } else if (res.result.ttsInterval == 600) {
          this.timename = "10分钟";
        } else if (res.result.ttsInterval == 1800) {
          this.timename = "30分钟";
        } else if (res.result.ttsInterval == 3600) {
          this.timename = "1小时";
        }
      } catch (e) {}
    },
    queren() {
      this.SetUserMsgCfg();
      uni.reLaunch({
        url: "/pages/PersonalCenter/setup",
      });
    },
  },
};
</script>

<style lang="scss">
.boxt_box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 25upx;
  background-color: #fff;
  border-bottom: 1px solid #e8e8e8;
}
.success_key {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 100upx;
  border-bottom: 1px solid #e8e8e8;
  .success_fl {
    padding: 25upx;
  }
  .success_fr {
    display: flex;
    align-items: center;
    text {
      color: #808080;
    }
    image {
      width: 32upx;
      height: 32upx;
      margin-left: 20upx;
      margin-right: 30upx;
    }
    .active {
      background-color: #ef4d3d;
      color: #fff;
    }
  }
}
.footer {
  width: 436upx;
  text-align: center;
  position: fixed;
  bottom: 70upx;
  left: 20%;
}
</style>
